import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:chewie/chewie.dart';

// final videoPlayerController = VideoPlayerController.network(
//     'https://www.runoob.com/try/demo_source/mov_bbb.mp4');

// final playerWidget = Chewie(
//   controller: chewieController,
// );

class Video extends StatefulWidget {
  final arguments;
  // String url = 'http://vt1.doubanio.com/202004290954/0b18be6e5a359ae412f7089a81b44233/view/movie/M/302530270.mp4';
  Video({Key key,this.arguments}) : super(key: key);

  @override
  _VideoState createState() => _VideoState(this.arguments['url']);
}

class _VideoState extends State<Video> {
  // Map arguments;
  String ceshi;
  String url = 'http://vt1.doubanio.com/202004290954/0b18be6e5a359ae412f7089a81b44233/view/movie/M/302530270.mp4';
  ChewieController chewieController;
  VideoPlayerController videoPlayerController;

  _VideoState(this.url);
  // @override
  // void initState() {
  //   super.initState();
  //   // print(this.ceshi);
  //   chewieController = ChewieController(
  //     videoPlayerController: VideoPlayerController.network(this.url),
  //     // videoPlayerController: videoPlayerController,
  //     aspectRatio: 3 / 2,
  //     autoPlay: true,
  //     looping: false,
  //     errorBuilder: (context, errorMessage) {
  //       return Center(
  //         child: Text(
  //           errorMessage,
  //           style: TextStyle(color: Colors.white),
  //         ),
  //       );
  //     },
  //   );
  // }
  //  @override
  // void dispose() {
  //   super.dispose();
  //   videoPlayerController.dispose();
  //   chewieController.dispose();
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: new AppBar(
          title: new Text("预告抢先看"),
          backgroundColor: Colors.blue,
          centerTitle: true,
        ),
        body: Container(
          child: Column(
            children: <Widget>[
              ChewieListItem(looping: true,videoPlayerController: VideoPlayerController.network(this.url),)
            //   Chewie(
            //     controller: chewieController,
            //   ),
            //   ButtonBar(
            //     children: <Widget>[
            //       new RaisedButton(
            //         child: new Text('点我'),
            //         onPressed: () {
            //           chewieController.play();
            //         },
            //       )
            //     ],
            //   )
            
            ],
          ),
        ));
  }
}

class ChewieListItem extends StatefulWidget {
  final VideoPlayerController videoPlayerController;
  final bool looping;
  ChewieListItem({@required this.videoPlayerController, this.looping, Key key})
      : super(key: key);
  @override
  _ChewieListItemState createState() => _ChewieListItemState();
}

class _ChewieListItemState extends State<ChewieListItem> {
  ChewieController _chewieController;

  @override
  void initState() {
    super.initState();

    _chewieController = ChewieController(
      videoPlayerController: widget.videoPlayerController,
      aspectRatio: 16 / 9,
      autoInitialize: true,
      looping: widget.looping,
      autoPlay: true,
      showControls: true,
      errorBuilder: (context, errorMessage) {
        return Center(
          child: Text(
            errorMessage,
            style: TextStyle(color: Colors.white),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          children: <Widget>[
            Chewie(
              controller: _chewieController,
            ),
          ],
        ));
  }

  @override
  void dispose() {
    super.dispose();
    widget.videoPlayerController.dispose();
    _chewieController.dispose();
  }
}

// class ChewieListItem  extends StatefulWidget {
//   // Deo({Key key}) : super(key: key);

//   final VideoPlayerController videoPlayerController;
//   final bool looping;

//   ChewieListItem ({@required this.videoPlayerController, this.looping, Key key})
//       : super(key: key);

//   @override
//   _DeoState createState() => _DeoState();
// }

// class _DeoState extends State<ChewieListItem> {
//   ChewieListItem _chewieListItem;

//   @override
//   void initState() {
//     super.initState();
//     _chewieListItem = ChewieListItem(
//         videoPlayerController: widget.videoPlayerController,

//         // aspectRatio: 16 / 9,
//         // autoInitialize: true

//     );
//   }

//   @override
//   Widget build(BuildContext context) {
//     return Container(
//       child: Text('sdv'),
//     );
//   }
// }

// Container(
//     child: Column(
//   children: <Widget>[
//     FutureBuilder(
//       future: _initializeVideoPlayerFuture,
//       builder: (context, snapshot) {
//         print(snapshot.connectionState);
//         if (snapshot.hasError) print(snapshot.error);
//         if (snapshot.connectionState == ConnectionState.done) {
//           return AspectRatio(
//             aspectRatio: 15 / 6,
//             // aspectRatio: _controller.value.aspectRatio,
//             child: VideoPlayer(_controller),
//           );
//         } else {
//           return Center(
//             child: CircularProgressIndicator(),
//           );
//         }
//       },
//     ),
//     ButtonBar(
//       children: <Widget>[
//         RaisedButton(
//             onPressed: () {
//               print(_controller);
//               setState(() {
//                 if (_controller.value.isPlaying) {
//                   _controller.pause();
//                 } else {
//                   _controller.play();
//                 }
//               });
//             },
//             child: new Text('点我')),
//              RaisedButton(
//             onPressed: () {
//               // print(_controller.value.position);
//             },
//             child: new Text('增加音量')),
//              RaisedButton(
//             onPressed: () {
//               // print(_controller.value.isBuffering);
//               _controller.setVolume(100);
//               print(_controller.value);
//             },
//             child: new Text('增加播放进度'))

//       ],
//     ),
//   ],
// ))
